package com.atguigu.gulimall.juc.demo;

public class SingletonDemo {
    // 禁止指令重排
    private static volatile SingletonDemo instance = null;
    private SingletonDemo(){

    }

    // DCL double check lock 双端检测锁机制

    /**
     * instance=new SingletonDem(); 可以分为以下步骤(伪代码) 指令重排存在
     *  
     * memory=allocate();//1.分配对象内存空间
     * instance(memory);//2.初始化对象
     * instance=memory;//3.设置instance的指向刚分配的内存地址,此时instance!=null 
     * @return
     */
    public static SingletonDemo getInstance(){
        if(instance == null){
            synchronized (SingletonDemo.class){
                if(instance == null){
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {

    }
}
